package com.xg.frame.base;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class BaseController {

	@SuppressWarnings("rawtypes")
	public Page getPage(HttpServletRequest request){
		Long page=Long.valueOf(request.getParameter("page"));
		Long limit=Long.valueOf(request.getParameter("limit"));
		return new Page(page,limit);
	}





	/**
	 * 将request参数值转为json
	 */
//	public JSONObject request2Json(HttpServletRequest request) {
//		JSONObject requestJson = new JSONObject();
//		Enumeration paramNames = request.getParameterNames();
//		while (paramNames.hasMoreElements()) {
//			String paramName = (String) paramNames.nextElement();
//			String[] pv = request.getParameterValues(paramName);
//			StringBuilder sb = new StringBuilder();
//			for (int i = 0; i < pv.length; i++) {
//				if (pv[i].length() > 0) {
//					if (i > 0) {
//						sb.append(",");
//					}
//					sb.append(pv[i]);
//				}
//			}
//			requestJson.put(paramName, sb.toString());
//		}
//		return requestJson;
//
//	}


	/**
	 * 将request参数值转为json
	 */
	public JSONObject request2Json(HttpServletRequest request) {
		JSONObject requestJson = new JSONObject();
		Enumeration paramNames = request.getParameterNames();
		while (paramNames.hasMoreElements()) {
			String paramName = (String) paramNames.nextElement();
			String[] pv = request.getParameterValues(paramName);
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < pv.length; i++) {
				if (pv[i].length() > 0) {
					if (i > 0) {
						sb.append(",");
					}
					sb.append(pv[i]);
				}
			}
			requestJson.put(paramName, sb.toString());
		}
		return requestJson;

	}

}
